package org.demo.baoleme.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleLogger {
    private static final String LOG_DIR = "logs";
    private static final String LOG_FILE = "app.log";
    private static final String LOG_PATH = LOG_DIR + File.separator + LOG_FILE;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    static {
        File dir = new File(LOG_DIR);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    private static void writeLog(String level, String message) {
        String time = sdf.format(new Date());
        String logMsg = String.format("[%s] [%s] %s%n", time, level, message);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_PATH, true))) {
            writer.write(logMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 新增：支持格式化字符串和多个参数的日志方法
    private static void writeLog(String level, String format, Object... args) {
        String message = String.format(format, args);
        writeLog(level, message);
    }

    // 新增：支持异常信息的日志方法
    private static void writeLog(String level, String message, Throwable throwable) {
        String time = sdf.format(new Date());
        String logMsg = String.format("[%s] [%s] %s%n", time, level, message);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_PATH, true))) {
            writer.write(logMsg);
            // 写入异常堆栈信息
            throwable.printStackTrace(new java.io.PrintWriter(writer));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 新增：支持格式化字符串、参数和异常的日志方法
    private static void writeLog(String level, String format, Throwable throwable, Object... args) {
        String message = String.format(format, args);
        writeLog(level, message, throwable);
    }

    // 以下为对外公开的日志方法，新增支持不同参数组合的重载方法

    public static void log(String message) {
        writeLog("LOG", message);
    }

    public static void debug(String message) {
        writeLog("DEBUG", message);
    }

    public static void info(String message) {
        writeLog("INFO", message);
    }

    // 新增：带格式化参数和模块名的info方法
    public static void info(String format, Object... args) {
        writeLog("INFO", format, args);
    }

    public static void warn(String message) {
        writeLog("WARN", message);
    }

    public static void error(String message) {
        writeLog("ERROR", message);
    }

    // 新增：带模块名的error方法
    public static void error(String message, String module) {
        writeLog("ERROR", String.format("%s [模块：%s]", message, module));
    }

    // 新增：带格式化参数和模块名的error方法
    public static void error(String format, Object... args) {
        writeLog("ERROR", format, args);
    }

    // 新增：带异常、消息和模块名的error方法
    public static void error(String message, Throwable throwable, String module) {
        writeLog("ERROR", String.format("%s [模块：%s]", message, module), throwable);
    }
}